package com.junxonline.instant.api.service.api.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yitter.idgen.YitIdHelper;
import com.junxonline.instant.api.service.api.ApiV1Service;
import com.junxonline.instant.common.util.api.MinioUtils;
import com.junxonline.instant.common.util.common.RedisUtils;
import com.junxonline.instant.dao.entity.dto.common.ServerTimeDTO;
import com.junxonline.instant.dao.entity.model.device.Terminal;
import com.junxonline.instant.dao.entity.vo.device.TerminalVO;
import com.junxonline.instant.dao.mapper.device.TerminalMapper;
import com.junxonline.instant.util.NetworkUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * API服务接口实现类
 *
 * @author JunX
 * @date 2021-11-12
 */
@Log4j2
@Service
@Transactional
public class ApiV1ServiceImpl implements ApiV1Service {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private TerminalMapper terminalMapper;

    /**
     * 获取服务器时间
     *
     * @return ServerTimeDTO
     */
    @Override
    public ServerTimeDTO getServerTime() {
        Date currentDate = DateUtil.date();
        String zone = TimeZone.getDefault().getID();
        String datetime = DateUtil.format(currentDate, "yyyy-MM-dd HH:mm:ss");
        Long time = currentDate.getTime();
        return new ServerTimeDTO(zone, datetime, time);
    }

    /**
     * 上报终端信息
     *
     * @param vo 请求参数
     */
    @Override
    public void uploadInformation(TerminalVO vo) {
        Date now = DateUtil.date();
        // 根据终端编号查询该终端是否被录入
        List<Terminal> list = terminalMapper.selectList(new LambdaQueryWrapper<Terminal>().eq(Terminal::getCode, vo.getCode()));
        boolean isInsert = false;
        Terminal terminal;
        // 已被录入则进行信息更新
        if (list != null && list.size() > 0) {
            terminal = list.get(0);
            terminal.setUpdateTime(now);
        }
        // 没有被录入则插入新记录
        else {
            isInsert = true;
            terminal = new Terminal();
            terminal.setId(YitIdHelper.nextId());
            terminal.setOrganizationId(1L);
            terminal.setName("未知(未分配)");
            terminal.setCreateTime(now);
            terminal.setStatus(0);
            terminal.setVersion(1);
        }
        terminal.setType(vo.getType());
        terminal.setCode(vo.getCode());
        terminal.setHardwareCode(vo.getHardwareCode());
        terminal.setResolution(vo.getResolution());
        terminal.setSoftwareVersion(vo.getSoftwareVersion());
        terminal.setOs(vo.getOs());
        terminal.setDiskSpace(vo.getDiskSpace());
        terminal.setIntranetIp(vo.getIntranetIp());
        terminal.setInternetIp(NetworkUtils.getIpAddress(request));
        terminal.setServerAddress(vo.getServerAddress());
        if (isInsert) {
            terminalMapper.insert(terminal);
        } else {
            terminalMapper.updateById(terminal);
        }
    }

}
